package weaver.formmode.customjavacode.modeexpand.xfjt.duss;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-06-13
 * @Description: 更新【办公用品库存】，库存表中不存在的插入，存在的累计加库存值
 * @ModifyLog:
 **/

public class WeaBgyprkmxExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User) param.get("user");
            //数据id
            int billid = -1;
            //模块id
            int modeid = -1;
            //模块id
            int userid = -1;
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                userid = Util.getIntValue(user.getUID());
                if (billid > 0 && modeid > 0) {
                    /*
                     *@Description:
                     */
                    RecordSet rs = new RecordSet();
                    rs.writeLog("WeaBgyprkmxExpand-----------------" + billid);
                    //插入主表
                    String sql = "select * from uf_bgyprkmx where id=?";
                    rs.executeQuery(sql, billid);
                    rs.next();
                    Map requestMainMap = new HashMap<>();
                    //名称浏览
                    String wpmc = Util.null2String(rs.getString("wpmc"));
                    //类别
                    String wplb = Util.null2String(rs.getString("wplb"));
                    //单位
                    String dw = Util.null2String(rs.getString("dw"));
                    //类型
                    String lx = Util.null2String(rs.getString("lx"));
                    int sl = Util.getIntValue(rs.getString("sl"),0);
                    boolean flag = isRepeatGoods(wpmc);
                    rs.writeLog("flag="+flag);
                    if (flag) {
                        requestMainMap.put("mc",wpmc);
                        requestMainMap.put("lb", wplb);
                        requestMainMap.put("dw",dw);
                        requestMainMap.put("kc",sl);
                        requestMainMap.put("mcwb",getGoodsName(wpmc));
                        requestMainMap.put("lx", lx);
                        insertModelData("uf_bgypkc",userid,requestMainMap);
                    }else {
                        rs.executeUpdate("update uf_bgypkc set kc=kc+"+sl+" where mc=?",wpmc);
                    }
                }
            }
        } catch (Exception e) {
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }
    /**
     * @Description: 将数据插入表单建模
     */
    public static int insertModelData(String tablename, int userid, Map map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        String uuid = UUID.randomUUID().toString();
        int modeid = getFormmodeId(tablename);
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if (rs.executeUpdate(insSql, params)) {
            rs1.executeQuery("select id from " + tablename + " where uuid=?", uuid);
            rs1.next();
            dataid = Util.getIntValue(rs1.getString("id"));
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get(key).toString() + "',";
                }
                if (updatesql.endsWith(",")) {
                    updatesql = updatesql.substring(0, updatesql.length() - 1);
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate(updatesql);
                }
                //进行权限重构

                getNewModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }
    /**
     * @Description: 建模权限重构
     */
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(userid, formmodeid, id);
    }

    /**
     * @Description: 获取formmodeid
     */
    public static int getFormmodeId(String tableName) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = ?", tableName);
        rs.next();
        int formmodeid = Util.getIntValue(rs.getString("id"));
        return formmodeid;
    }
    /**
     * @Description: 物品名称重复校验
     */
    public static boolean isRepeatGoods(String mc) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(id) from uf_bgypkc where mc=?", mc);
        rs.next();
        int count = Util.getIntValue(rs.getString(1));
        return count <= 0;
    }
    /**
     * 获取物品名称
     *
     * @param mc
     * @return
     */
    public static String getGoodsName(String mc) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select wzmc from uf_wzmc where id=?", mc);
        rs.next();
        return Util.null2String(rs.getString("wzmc"));
    }
}
